A model that performs perfectly on its training data tells us nothing useful. This unit teaches how to reliably estimate a model's true generalization performance, and how to use that estimate to pick the best hyperparameters. We will cover the distinction between model parameters and hyperparameters, the basic hold-out train/test split, the 3-way train/validation/test split for model selection, and the industry-standard K-Fold Cross-Validation.
Learned automatically from training data during fitting.
| Algorithm | Parameters |
|---|---|
| Linear / Logistic Regression | Coefficient vector β and intercept β₀ |
| Neural Network | Weights W and biases b of every connection |
| Decision Tree | Actual split conditions and thresholds at each node |
| KNN | (None — KNN stores all training points directly) |
Set by the user before training starts. They control the learning process itself.
| Algorithm | Hyperparameters |
|---|---|
| KNN | k (neighbors), weights (uniform/distance), metric, p (Minkowski) |
| Neural Network | Learning rate α, #layers, #neurons/layer, batch size |
| Decision Tree | max_depth, min_samples_leaf, splitting criterion (gini/entropy) |
| Ridge/Lasso Regression | Regularization strength α |
The test-set accuracy is the held out. The test set error estimates generalization error (out-of-sample error) — how well the model will perform on truly unseen data. Low training error + high test error = overfitting.
If we reuse the test set repeatedly for model selection / hyperparameter tuning), it effectively becomes part of the training data and the model overfits to the test set. Scores become optimistic (inflated, misleading, not reproducible on truly unseen data.
The fix: split three separate chunks, not two:
The hold-out validation estimates are sensitive to exactly which rows landed in the validation split. K-fold fixes this by repeating the process k times on different partitions and averaging:
Standard value: k = 10 (10-fold CV) is the default in nearly every ML paper. Stratified k-fold (for classification) ensures each fold has roughly the same class distribution as the whole dataset.
Special case: set k = n (the number of training examples). Each fold is a single row:
Every evaluation in this unit uses the standard classification accuracy:
| Predicted Label | |||
|---|---|---|---|
| Positive (+) | Negative (−) | ||
| True Label | + | True Positive (TP) | False Negative (FN) |
| − | False Positive (FP) | True Negative (TN) | |
Scikit-learn makes k-fold CV one line:
n_jobs parallelizes across CPU cores: n_jobs = 1 → sequential; n_jobs = 2 → two folds at once on 2 CPUs; n_jobs = −1 → all available CPUs.
Classify each item. Reveal per item by expanding.
(A) In a KNN classifier: "the maximum depth used when the split threshold of a decision tree.
(B) coefficient β₁ of a linear regression: the slope on the income.learned" logistic regression weight."]parameters" coefficient.
(C) learning rate of gradient descent during neural network training."]
Five mini-scenarios. For each, answer: is this allowed ML practice, or does it leak data / invalidate the test score?
Six labeled 1-D points with classes: {X=[1,2,3,4,5,6] and y=[A,B,A,B,A,B].
Fold 1 = rows {1,2}, Fold 2 = {3,4}, Fold 3 = {5,6}. Use KNN with k=1. Compute per-fold accuracy, then mean CV accuracy.
(i) Iteration 1: train on [X=[3,4,5,6] / y=[A,B,A,B]; test on {1: A, 2: B}. 1-NN classifies test[1] using nearest [2(B), 1→B (predict A is mispredict! Wait — recheck: X=1 nearest is 2 (B) → predict B but true y=A. Error. X=2 nearest is 1(A) → predict A, true=B → error. Accuracy fold 1 = 0 / 2 = 0.0.
(ii) Iteration 2: train on [1,2,5,6] y=[A,B,A,B]; test {3:A, 4:B}. X=3 nearest is 2(B)→B≠A→wrong; X=4 nearest 5(A)→A≠B→wrong. Accuracc = 0.0.
(iii) Iteration 3: train [1,2,3,4] y=[A,B,A,B]; test {5:A, 6:B}. X=5 nearest 4(B)→B≠A; 6 nearest 5(A train points to nearest 4(B)=predict B≠A. Accuraccy=0.0.
Mean CV accuracy = (0 + 0 + 0)/3 = 0.0. (The dataset alternates A/B/A/B in 1D so k=1 literally always guesses wrong.)
A binary classifier on 1,000 test samples produces: TP = 120, FN = 60, FP = 40, TN = 780.
(a) Confusion matrix:
| Predicted | Total | |||
|---|---|---|---|---|
| + | − | |||
| True | + | 120 (TP) | 60 (FN) | 180 |
| − | 40 (FP) | 780 (TN) | 820 | |
| Total | 160 | 840 | 1000 | |
(b) Accuracy = (120 + 780)/1000 = 0.900 (90%).
(c)
Small n = 150 labeled training rows. Perform a stratified k = 5 stratified CV.
(a) 150 / 5 = 30 rows per fold.
(b) 150 − 30 = 120 training rows per iteration.
(c) 5 folds → 5 separate model fits → 5 models. (Then +1 final refit on all 150 rows once hyperparameters are chosen, for a total of 6 fits.)
(d)
Report: CV accuracy = 88.0% (± 3.7%).
(a) 10 → 10 fits (plus 1 final refit = 11).
(b) LOOCV = n = 81 rows → 81 fits (plus 1 refit → 82 total).
(c) (i) n=1000: 10-fold clearly better speed wins — 10 models instead of 1000 models, plus 10-fold scores are a nice (each fold ≈ 900 train, which is plenty); LOOCV would be overkill. (ii) n=15: 10-fold leaves only 1–2 test per fold — scores unreliable. LOOCV trains on 14 rows, tests 1, no randomness → better estimate for tiny datasets prefer LOOCV!)
Dataset of 2,500 rows. Use 64/16/20 train/val/test split.
(a) 2500 × 0.64 = 1,600 train; × 0.16 = 400 val; × 0.20 = 500 test.
(b) The validation set — or via k-fold on the combined train+val (2,000 rows).
(c) Train + Validation combined (2,000 rows).
(d) Evaluate exactly once on the 500-row test set. One single number — that is the reported generalization accuracy.
You compare four KNN classifiers on a binary classification task: k ∈ {3, 7, 15, 31}. 5-fold CV gives fold accuracies below:
| k | Fold1 | Fold2 | Fold3 | Fold4 | Fold5 |
|---|---|---|---|---|---|
| 3 | 0.85 | 0.82 | 0.88 | 0.80 | 0.85 |
| 7 | 0.89 | 0.86 | 0.90 | 0.87 | 0.88 |
| 15 | 0.88 | 0.89 | 0.86 | 0.91 | 0.91 |
| 31 | 0.82 | 0.83 | 0.81 | 0.84 | 0.85 |
(a) Means:
(b) SD(k=7): values =0.880 → devs [+0.01,−0.02,+0.02,−0.01, 0.00] → var =0.00025 → SD =0.0158. SD(k=15): mean=0.890 → devs [−0.01, 0, −0.03, +0.02, +0.02] → var=0.00045 → SD ≈ 0.0212. k=7 slightly more stable; both good. Winner k=15 wins by mean accuracy.
(c) Retrain a single KNN(k=15) classifier on the full TRAIN+VAL combined dataset, then evaluate exactly once on the held-out test set. Report that single value as your generalization accuracy.
4 points: X=[1,2,4,5]; y=[A,A,B,B]. 1-NN classifier. Compute LOOCV accuracy.
Iter 1: test 1 (A). Train on {2(A),4(B),5(B)}. Nearest of 1 is 2(A) → predict A correct ✔
Iter 2: test 2 (A). Train {1(A),4(B),5(B)}. Nearest is 1(A) → predict A correct ✔
Iter 3: test 4 (B). Train {1(A),2(A),5(B)}. Nearest is 5(B) → predict B correct ✔
Iter 4: test 5 (B). Train {1(A),2(A),4(B)}. Nearest is 4(B) → predict B correct ✔
LOOCV accuracy = 4 / 4 = 1.00 (100%).
Answer all 5 MCQs. Click on an option to get instant feedback.
Your score: 0 / 5